package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.Bean;
import com.sgb.kjwl.model.entity.NeedDetailsEntity;
import com.sgb.kjwl.model.entity.enterprise.NeedDetailSignUpEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 添加公示
 */
public class PublishPublicityViewModel extends AndroidViewModel {

    public MutableLiveData<String> choicePublicity = new MutableLiveData<>();//是否公示
    public MutableLiveData<String> daysize = new MutableLiveData<>();//公示天数
    public MutableLiveData<String> note = new MutableLiveData<>();//中选说明
    public MutableLiveData<String> stringLiveData = new MutableLiveData<>();
    public MutableLiveData<String> ToastShow = new MutableLiveData<>();
    //1show,2dismiss
    public MutableLiveData<Integer> showloading = new MutableLiveData<>();

    public NeedDetailSignUpEntity mItemData = null;
    public NeedDetailsEntity needData = null;

    public List<Bean> listType = new ArrayList<>();
    public String file = "";
    public String filePath = "";
    public String fileName;
    public String fileSize;
    public String fileType;


    public PublishPublicityViewModel(@NonNull @NotNull Application application) {
        super(application);
    }


    /**
     * 提交数据
     */

    public void submitData() {

        if (TextUtils.isEmpty(choicePublicity.getValue())) {
            ToastShow.postValue("请选择是否公示");
            return;
        }

        if (choicePublicity.getValue().equals("是") && TextUtils.isEmpty(daysize.getValue())) {
            ToastShow.postValue("请输入天数");
            return;
        }

        if (choicePublicity.getValue().equals("是") && Integer.parseInt(daysize.getValue()) == 0) {
            ToastShow.postValue("至少公示1天");
            return;
        }

        if (choicePublicity.getValue().equals("是") && Integer.parseInt(daysize.getValue()) > 30) {
            ToastShow.postValue("最多公示30天");
            return;
        }
        if (TextUtils.isEmpty(note.getValue())) {
            ToastShow.postValue("请输入中选说明");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("whetherPublic", choicePublicity.getValue().equals("是") ? "0" : "-1");
        map.put("publicityPeriodDay", daysize.getValue());
        map.put("electionShow", note.getValue());
        if (!TextUtils.isEmpty(stringLiveData.getValue())) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("name", fileName);
            jsonObject.addProperty("url", stringLiveData.getValue());
            jsonObject.addProperty("size", fileSize);
            jsonObject.addProperty("type", fileType);
            map.put("notificationPath", jsonObject.toString());
        }
        map.put("offerId", mItemData.getId());
        map.put("electionName", mItemData.getName());
        map.put("demandId", needData.id + "");
        switch (mItemData.getUsertype()) {
            case 0:
                map.put("electionNo", mItemData.getUserNo());
                break;
            case 1:
                map.put("electionNo", mItemData.getCompanyNo());
                break;
            case 2:
                map.put("electionNo", mItemData.getTeamNo());
                break;
        }
//        Log.e("sendMessage++++======================",new Gson().toJson(map));
        showloading.postValue(1);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().saveQuotationOffer(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                showloading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    showloading.postValue(3);
                } else {
                    ToastShow.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                showloading.postValue(4);

            }
        });


    }

    /****
     * 上传单张图片
     * @param file
     * @param completeNum
     */
    public void uploadSingleImage(Context context, File file, int completeNum, boolean isEnd) {
        Map<String, RequestBody> photos = new HashMap<>(1);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(context, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFileV1(photos).enqueue(new Callback<BaseEntity<List<String>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() != 0) {
                        stringLiveData.postValue(response.body().getData().get(0));
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public void initListType() {
        listType.clear();
        listType.add(new Bean(0, "是"));
        listType.add(new Bean(1, "否"));

    }


}
